package com.bit.book.controller;

import com.bit.book.constants.Constants;
import com.bit.book.model.*;
import com.bit.book.service.BookService;
import jakarta.servlet.http.HttpSession;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;

@Slf4j
@RequestMapping("/book")
@RestController
public class BookController {
    @Autowired
    BookService bookService;
    @RequestMapping("/getList")

    public List<BookInfo> getList(){
        List<BookInfo> bookInfos = bookService.getList();
        return bookInfos;
    }

    @RequestMapping("/addBook")
    public String addBook(BookInfo bookInfo){
     //   log.info("addbookInfo:{}",bookInfo);
        if (!StringUtils.hasLength(bookInfo.getBookName())
        ||!StringUtils.hasLength(bookInfo.getAuthor())
        ||bookInfo.getCount()==null
        ||bookInfo.getPrice()==null
        ||!StringUtils.hasLength(bookInfo.getPublish())
        ||bookInfo.getStatus()==null) {
            return "error:输入参数不合法，请检查入参";
        }
        try {
            bookService.addBook(bookInfo);
            return "";
        } catch (Exception e) {
          //  log.error("添加图书失败:",e);
            return e.getMessage();
        }
    }
    @RequestMapping("/getListPage")
    public Result getListByPage(PageRequest pageRequest, HttpSession session) {
       log.info("获取图书列表,pageRequest:{}",pageRequest);
       if(session.getAttribute(Constants.SESSION_USER_KEY)==null){
           return Result.unLogin();
       }
        UserInfo userInfo = (UserInfo) session.getAttribute(Constants.SESSION_USER_KEY);
       if(userInfo==null||userInfo.getId()<0||"".equals(userInfo.getUserName())){
           return Result.unLogin();
       }
        PageResult<BookInfo> pageResult=
                bookService.getBookListByPage(pageRequest);
        return Result.success(pageResult);
    }
    @RequestMapping("/queryBookById")
    public BookInfo queryBookById(@RequestParam(value = "bookId") String id1) {
        Integer id=Integer.valueOf(id1);
        log.info("queryBookById{}",id);
        if(id==null||id<=0 ) {
            return new BookInfo();
        }
        BookInfo bookInfo=bookService.queryBookById(id);
        log.info("queryBookById:{}",bookInfo);
            return bookInfo;

    }
    @RequestMapping("/updateBook")
    public String updateBook(BookInfo bookInfo) {
        log.info("updateBook:{}",bookInfo);
        try {
            bookService.updateBook(bookInfo);
            return "";
        } catch (Exception e) {
            log.error("修改图书失败",e);
            return e.getMessage();
        }

    }
    @RequestMapping("/batchDeleteBook")
    public Boolean batchDeleteBook(@RequestParam List<Integer> ids) {
        log.info("batchDeleteBook:{}",ids);
        try {
            bookService.batchDeleteBook(ids);
        } catch (Exception e) {
            log.error("批量删除异常{}",e);
            return false;
        }
        return true;
    }

}
